<?php

namespace Helpers;

use Carbon\Carbon;
use DateTime;

/* * ***************************************************************************
 * Helpers\Helper
 * 
 * author  Laureta Xhaferaj
 * email   laureta@first.al
 * 
 * @function fa_link_to_action
 * @function icon_link_to_route
 * **************************************************************************** */

class Helper {

    /**
     * Create  link to named action with icon.
     * 
     * author  Laureta Xhaferaj
     * 
     * @param  string $icon
     * @param  string $action
     * @param  string $text
     * @param  array  $parameters
     * @param  array  $attributes
     * @return string
     */
    public static function fa_link_to_action($icon, $action, $title = null, $parameters = array(), $attributes = array()) {
        $url = action($action, $parameters);
        $attributes = \HTML::attributes($attributes);

        $title = (is_null($title)) ? $url : '<i class="' . $icon . '"></i> '.$title;

        return '<a href="' . $url . '"' . $attributes . '>' . $title . '</a>';
    }

    public static function modal($title, $btn1, $btn2, $id_confirm) {
        $content = '<div id="dataConfirmModal" class="modal fade" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">' . $title . '</h4></div><div class="modal-body"></div><div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">' . $btn1 . '</button>
                    <a class="btn btn-primary" id="' . $id_confirm . '">' . $btn2 . '</a>
                    </div>
                    </div>
                </div>
            </div>';
        return $content;
    }

    /**
     * Create  link to named route with glyphicon icon.
     * 
     * author  Laureta Xhaferaj
     * 
     * @param  string $icon
     * @param  string $route
     * @param  string $title
     * @param  array  $parameters
     * @param  array  $attributes
     * @return string
     */
    function icon_link_to_route($icon, $route, $title = null, $parameters = array(), $attributes = array()) {
        $url = route($route, $parameters);

        $title = (is_null($title)) ? $url : e($title);

        $attributes = HTML::attributes($attributes);

        $title .= '<span class = "glyphicon glyphicon-' . e($icon) . '"></span>';

        return '<a href = "' . $url . '"' . $attributes . '>' . $title . '</a>';
    }

    /**
     * Create  date to  input.
     * 
     * author  Laureta Xhaferaj
     * @param  string $name
     * @param  string $value
     * @param  array $options
     * @return string
     */
    public static function date($name, $value = null, $options = array()) {
        $input = '<input type = "date" name = "' . $name . '" value = "' . $value . '"';
        foreach ($options as $key => $value) {
            $input .= ' ' . $key . ' = "' . $value . '"';
        }
        $input .= '>';

        return $input;
    }

    /**
     * Create  date to  input.
     * 
     * author  Laureta Xhaferaj
     * @param  string $name
     * @param  string $value
     * @param  array $options
     * @return string
     */
    public static function time($name, $value = null, $options = array()) {
        $input = '<input type = "time" name = "' . $name . '" value = "' . $value . '"';
        foreach ($options as $key => $value) {
            $input .= ' ' . $key . ' = "' . $value . '"';
        }
        $input .= '>';

        return $input;
    }

    public static function date_bootstrap($name, $id, $value = null) {
        $input = '<div class = "col-lg-3">';
        $input .= '<div class = "input-group input-append date" name = "' . $name . '" id = "' . $id . '" data-date = "12/12/2012" data-date-format = "dd-mm-yyyy">';
        $input .= '<input class = "form-control" type = "text" value = "' . $value . '" readonly = "">';
        $input .= '<span class = "input-group-addon add-on"><i class = "fa fa-calendar"></i></span> ';
        $input .= '</div></div>';
        return $input;
    }

    public static function macro($errors) {
        return "<span class=\"glyphicon glyphicon-warning-sign\">"
                . "</span> $errors";
    }

    public static function mul($value1, $value2) {
        $result = 0;
        $result = $value1 * $value2;
        return $result;
    }

    /**
     * Gets result about status of vehicle.
     * 
     * author  Laureta Xhaferaj
     * @param  string $qry
     * @param  string $date
     * @param  string $limit_date
     * 
     * @return string result
     */
    public static function status($qry, $date, $limit_date) {
        $result = 0;
        if ($qry == 0000 - 00 - 00) {
            $result += 0;
        } elseif ($qry > $limit_date) {
            $result += 1;
        } else if (($qry >= $date) && ($qry <= $limit_date )) {
            $result += 5;
        } else if ($qry < $date && $qry != 0000 - 00 - 00) {
            $result += 21;
        }
        return $result;
    }

    /**
     * Sum of results that gets from status() to display final status.
     * 
     * author  Laureta Xhaferaj
     * @param  string $qry1
     * @param  string $qry2
     * @param  string $qry3
     * @param  string $qry4
     * @param  string $limit_date
     * 
     * @return string sum
     */
    public static function sum($qry1, $qry2, $qry3, $qry4, $date, $limit_date) {
        $sum = 0;
        $result1 = Helper::status($qry1, $date, $limit_date);
        $result2 = Helper::status($qry2, $date, $limit_date);
        $result3 = Helper::status($qry3, $date, $limit_date);
        $result4 = Helper::status($qry4, $date, $limit_date);

        $sum += $result1 + $result2 + $result3 + $result4;
        return $sum;
    }

    /**
     * Create  link to named route with image
     * 
     * author  Laureta Xhaferaj
     * @param  string $action
     * @param  string $img
     * @param  string $alt
     * @param  array $parameters
     * @param  array $attributes
     * 
     * @return string
     */
    public static function image_link($action, $img = '', $alt = '', $parameters = array(), $attributes = array()) {
        $url = action($action, $parameters);

        $img = \HTML::image($img, $alt);

        $attributes = \HTML::attributes($attributes);

        return '<a href = "' . $url . '"' . $attributes . '>' . $img . '</a>

         

          

          

          ';
    }

    /**
     * Create  link to named route with image
     * 
     * author  Laureta Xhaferaj
     * @param  string $v1
     * @param  string $v2
     * 
     * @return string
     */
    public static function next_date($v1, $v2) {
        $next_date = Carbon::instance(new DateTime($v1))->addMonths($v2);
        return $next_date;
    }

    /**
     * Gets result about status of vehicle maintenance.
     * 
     * author  Laureta Xhaferaj
     * @param  string $current_milage
     * @param  string $min_km_due
     * @param  string $maintenance_milage
     * 
     * @return string result
     */
    public static function status_maintenance($current_milage, $km_due, $maint_milage) {
        $result = 0;
        $next_milage = $km_due + $maint_milage;
        if ($current_milage < $next_milage - 1000) {
            $result += 1;
        } else if (($current_milage >= $next_milage - 1000) && ($current_milage <= $next_milage )) {
            $result += 5;
        } else if ($current_milage > $next_milage) {
            $result += 21;
        }
        return $result;
    }

    /**
     * Gets result about status of vehicle maintenance.
     * 
     * author  Laureta Xhaferaj
     * @param  string $current_date
     * @param  string $next_date
     * @param  string $before_date
     * 
     * @return string result
     */
    public static function status_date($current_date, $month_due, $maint_date) {
        $result = 0;
        $next_date = Carbon::instance(new DateTime($maint_date))->addMonths($month_due);
        $before_date = Carbon::instance(new DateTime($next_date))->subMonths(1);
        if ($current_date < $before_date) {
            $result += 1;
        } else if (($current_date >= $before_date) && ($current_date <= $next_date )) {
            $result += 5;
        } else if ($current_date > $next_date) {
            $result += 21;
        }
        return $result;
    }

    public static function array_value($value, $array) {
        $results = 0;
        $str = "$value";
        if (in_array($str, $array)) {
            $results += 1;
        } else {
            $results += 0;
        }
        return $results;
    }
        

}